package com.xrh.core.generater.mybatisplus;

import java.io.File;
import java.io.IOException;
import java.util.Map;
import java.util.Set;

import com.xrh.core.generater.bean.ColumnBean;
import com.xrh.core.generater.bean.TableInfo;
import com.xrh.core.generater.bean.UserInput;
import com.xrh.core.generater.interfaces.TemplateRootPath;
import com.xrh.core.generater.utils.FileUtils;
import com.xrh.core.generater.utils.MarkEnum;
import com.xrh.core.generater.utils.TemplateNameEnum;
import com.xrh.core.generater.utils.TemplateReader;

//AddRequest文件生成器
public class AddRequestGenerater {

		//生成文件
		public static void generateFile(UserInput userInput , TableInfo tableInfo , TemplateRootPath templateRootPath) throws IOException{
			
			String templateTxt = generateTemplateTxt(userInput, tableInfo , templateRootPath);
			
			FileUtils.generateFile(userInput.getOutputDirPath() + File.separator + "request" + File.separator + "addRequest", tableInfo.getClassName() + "AddRequest.java", templateTxt);
	
		}
		
		/**
		* @function 生成模板字符串
		* @author 肖荣辉
		* @date 2018年10月23日
		*/
		public static String generateTemplateTxt(UserInput userInput , TableInfo tableInfo , TemplateRootPath templateRootPath) throws IOException{
			
			String templateTxt =  TemplateReader.readTextTemplate(templateRootPath , TemplateNameEnum.AddRequest模板.name());
			
			//通用替换
			templateTxt = MarkEnum.replaceMark(templateTxt , userInput , tableInfo);
			
			//私有属性文本
			templateTxt = MarkEnum.replace(MarkEnum.PRIVATE_PROPERTY_MARK , templateTxt , generatePrivatePropertyTxt(tableInfo));
			
			return templateTxt;
		}
		

		//生成私有属性文本
		private static String generatePrivatePropertyTxt(TableInfo tableInfo) {
			
			Set<Map.Entry<String, ColumnBean>> set = tableInfo.getPropertyInfoMap().entrySet();
			
			StringBuilder sb = new StringBuilder();
			
			 String tab1 = "\n\t";
			 
			 String txt = 	tab1 + "/**"+MarkEnum.PRIVATE_PROPERTY_COMMENT_MARK.name()+" */"+
					 				MarkEnum.API_MODEL_PROPERTY_MARK.name() +
					 				tab1 + "private "+MarkEnum.PROPERTY_JAVA_TYPE_NAME_MARK.name()+ " "+MarkEnum.PROPERTY_NAME_MARK.name()+";";
			  
			for(Map.Entry<String, ColumnBean> item : set){
				
				  String key = item.getKey();
				  ColumnBean bean = item.getValue();
				  
				  //若是主键则跳过
				  if(bean.isPrimaryKey())
					  continue;
				  
				  sb.append(txt.replaceAll(MarkEnum.PRIVATE_PROPERTY_COMMENT_MARK.name(), bean.getComment())
						  			.replaceAll(MarkEnum.PROPERTY_JAVA_TYPE_NAME_MARK.name(), bean.getJavaTypeName())
						  			.replaceAll(MarkEnum.API_MODEL_PROPERTY_MARK.name(), generateAnnotationTxt(tab1, bean))
						  			.replaceAll(MarkEnum.PROPERTY_NAME_MARK.name(), key)).append("\n");
				  
			}
			
			return sb.toString().trim();
			
		}
		
		/**
		 * @function 生成注解标记
		 * @author 肖荣辉
		 * @date 2021年6月29日
		*/
		private static String generateAnnotationTxt(String tab , ColumnBean columnBean) {
			
			String txt = "";
			
			txt+= tab + "@ApiModelProperty(\""+columnBean.getComment()+"\")";
			
			if("String".equals(columnBean.getJavaTypeName())) {
				txt+= tab + "@NotBlank(message = \""+columnBean.getComment()+"不能为空\")";
			}else {
				txt+= tab + "@NotNull(message = \""+columnBean.getComment()+"不能为空\")";
			}
			
			return txt;
		}
		
}
